<?php
/**
 * @Name: UserController.php
 * @Author: yashuai
 */

namespace App\Http\Controllers\Rbac;

use App\Exceptions\ApiException;
use Illuminate\Http\Request;
use App\Http\Logic\Rbac\UserLogic;
use App\Http\Logic\Rbac\NodeLogic;
use App\Http\Search\Rbac\UserSearch;
use App\Http\Requests\Rbac\UserIDRequest;
use App\Http\Requests\Rbac\UserSaveRequest;
use App\Http\Requests\Rbac\UserPasswordRequest;
use Illuminate\Support\Facades\DB;

class UserController
{

    /**
     * @Desc 用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author yashuai
     */
    public function index(Request $request)
    {
        $result = UserSearch::instance($request->all())->index();
        return _success($result);
    }

    /**
     * @Desc 添加
     * @param UserSaveRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function create(UserSaveRequest $request)
    {
        $data = $request->getValidationData([
            'UserPassword' => 'required|between:6,32|alpha_dash'
        ]);
        return DB::transaction(function () use ($data) {
            $result = UserLogic::instance($data)->create();
            return _success($result);
        });
    }

    /**
     * @Desc 编辑
     * @param UserSaveRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function update(UserSaveRequest $request)
    {
        $data = $request->getValidationData([
            'UserID' => 'required|integer'
        ]);
        return DB::transaction(function () use ($data) {
            $result = UserLogic::instance($data)->update();
            return _success($result);
        });
    }

    /**
     * @Desc 删除
     * @param UserIDRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function delete(UserIDRequest $request)
    {
        UserLogic::instance($request->getValidationData())->delete();
        return _success();
    }

    /**
     * @Desc 用户信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws ApiException
     * @author yashuai
     */
    public function info(Request $request)
    {
        $params = [
            'UserID' => $request->userId
        ];
        $info   = UserLogic::instance($params)->info();
        return _success($info);
    }

    /**
     * @Desc 重置密码
     * @param UserPasswordRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function resetPassword(UserPasswordRequest $request)
    {
        UserLogic::instance($request->getValidationData())->resetPassword();
        return _success();
    }


    /**
     * @Desc 获取用户菜单节点
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function menuNode(Request $request)
    {
        $params = [
            'UserID' => $request->userId
        ];
        $node   = NodeLogic::instance($params)->getUserAdminNodeTree();
        return _success($node);
    }

}
